package com.ledim.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
import com.letv.android.young.client.R;

/**
 * Created by ccx on 16/8/1.
 */
public class RoundTextView extends TextView {

    private Paint strokePaint;
    private int strokeColor;
    private float strokeAlpha = 1;
    private static final int AUTO_COLOR = -2;
    private float strokeWidth;
    private float cornerRadius;
    private boolean isFill;

    private final RectF rectF = new RectF();

    public RoundTextView(Context context) {
        this(context, null);
    }

    public RoundTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 设置自定义字体
        setTypeface(Typeface.SANS_SERIF);
        if (attrs != null) {
            TypedArray typedArray =
                    context.obtainStyledAttributes(attrs, R.styleable.RoundTextView);
            // 不设置或设置为透明，则不绘制，设置为auto则取TextView颜色，
            strokeColor = typedArray.getColor(R.styleable.RoundTextView_stroke_color, 0);
            if (strokeColor == AUTO_COLOR) {
                // auto 设置auto时，取TextView textColor
                strokeColor = getCurrentTextColor();
            }
            strokeAlpha = typedArray.getFloat(R.styleable.RoundTextView_stroke_alpha, 1);
            isFill = typedArray.getBoolean(R.styleable.RoundTextView_is_fill, false);
            strokeWidth =
                    typedArray.getDimensionPixelSize(R.styleable.RoundTextView_stroke_width, 0);
            cornerRadius =
                    typedArray.getDimensionPixelSize(R.styleable.RoundTextView_corner_radius, 0);
            typedArray.recycle();
        }

        initPaint();
    }

    private void initPaint() {
        if (strokePaint == null) {
            strokePaint = new Paint();
            strokePaint.setAntiAlias(true);
        }

        resetStrokeWidth();
        resetStrokeAlpha();
        resetStrokeColor();
        resetFill();
    }

    /**
     * 设置是否填充
     *
     * @param isFill
     */
    public RoundTextView setFill(boolean isFill) {
        if (this.isFill != isFill) {
            this.isFill = isFill;
            resetFill();
        }
        return this;
    }

    /**
     * 设置边框或填充色
     *
     * @param strokeColor
     * @return
     */
    public RoundTextView setStrokeColor(int strokeColor) {
        if (this.strokeColor != strokeColor) {
            this.strokeColor = strokeColor;
            resetStrokeColor();
            //invalidate();
        }
        return this;
    }

    /**
     * @param strokeAlpha
     *         0-1
     * @return
     */
    public RoundTextView setStrokeAlpha(float strokeAlpha) {
        if (this.strokeAlpha != strokeAlpha) {
            this.strokeAlpha = strokeAlpha;
            resetStrokeAlpha();
        }
        return this;
    }

    /**
     * 设置边框宽度
     *
     * @param strokeWidth
     *         px
     * @return
     */
    public RoundTextView setStrokeWidth(float strokeWidth) {
        if (this.strokeWidth != strokeWidth) {
            this.strokeWidth = strokeWidth;
            resetStrokeWidth();
        }
        return this;
    }

    /**
     * 设置圆角
     *
     * @param cornerRadius
     * @return
     */
    public RoundTextView setCornerRadius(float cornerRadius) {
        this.cornerRadius = cornerRadius;
        return this;
    }

    /**
     * 如果设置各种属性前，该view已经绘制，就必须手动重绘（比如点击变色等场景）
     * listview item复用必须要手动invalidate
     */
    public void invokeConfig() {
        invalidate();
    }

    private void resetStrokeWidth() {
        strokePaint.setStrokeWidth(strokeWidth);
    }

    private void resetStrokeColor() {
        strokePaint.setColor(strokeColor);
    }

    private void resetStrokeAlpha() {
        strokePaint.setAlpha((int) (255 * strokeAlpha));
    }

    private void resetFill() {
        strokePaint.setStyle(isFill ? Paint.Style.FILL_AND_STROKE : Paint.Style.STROKE);
    }

    private void initRectF() {
        float offset = strokeWidth / 2;
        rectF.set(offset, offset, getWidth() - offset, getHeight() - offset);
    }

    /**
     * 是否需要绘制边框或填充
     *
     * @return
     */
    private boolean checkNeedDraw() {
        if (strokeColor == 0) {
            return false;
        }

        if (isFill) {
            if (strokeWidth == 0) {
                strokeWidth = 1f;
            }
            return true;
        }
        return strokeWidth != 0;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (checkNeedDraw()) {
            // 绘制矩形，把paint的宽度考虑进来
            initRectF();
            canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, strokePaint);
        }
        super.onDraw(canvas);

    }
}

